package TransportesMarroquin;

import java.awt.event.MouseEvent; 
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Gasto extends javax.swing.JFrame {

    ResultSetTableModel tableModel = null;
    String consulta;
    int num=0;
    
    String tipo=new String();
    private String file;
    int mesinicial=0;
    int mesfinal=0;
    int kilometraje;
   
  public Gasto(String cont){
     
       this.consulta=cont;
      
       initComponents();
       cgra.setVisible(false);
       bnew.setVisible(false);
  }
    public Gasto(String cont,int cod,String tipo,int kilometraje){
    // gastos especificos de camiones y cabezales 
       this.consulta=cont;
       this.tipo=tipo;
       this.kilometraje=kilometraje;
       initComponents();
       
       bcon.setVisible(false);
       con1.setVisible(false);
       con2.setVisible(false);
       con3.setVisible(false);
       con4.setVisible(false);
       
       this.num=cod;
       lnum.setText(""+num);
       
       
       
        
    }
    public Gasto(String cont,int cod,String tipo){
    // gastos especificos de camiones y cabezales 
       this.consulta=cont;
       this.tipo=tipo;
       
       initComponents();
       
       bcon.setVisible(false);
       con1.setVisible(false);
       con2.setVisible(false);
       con3.setVisible(false);
       con4.setVisible(false);
       
       this.num=cod;
       lnum.setText(""+num);
       
       
       
        
    }

    private Gasto() {
        
    }
  
  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        bnew = new javax.swing.JButton();
        bcon = new javax.swing.JButton();
        con2 = new javax.swing.JComboBox();
        con1 = new javax.swing.JLabel();
        con3 = new javax.swing.JLabel();
        con4 = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        lnum = new javax.swing.JLabel();
        lnum1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        cgra = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        try{
            tableModel = new ResultSetTableModel( consulta );
        }catch(Exception e){
            System.out.println("ERROR");
        }
        resultTable.setModel(tableModel);
        resultTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        resultTable.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        resultTable.setSelectionBackground(new java.awt.Color(255, 255, 255));
        resultTable.setSelectionForeground(new java.awt.Color(0, 0, 0));
        resultTable.getTableHeader().setReorderingAllowed(false);
        resultTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                resultTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(resultTable);

        bnew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/1316277158_filenew.png"))); // NOI18N
        bnew.setText("Nuevo ");
        bnew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnewActionPerformed(evt);
            }
        });

        bcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/consultar.png"))); // NOI18N
        bcon.setText("Consultar");
        bcon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bconActionPerformed(evt);
            }
        });

        con2.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
            "None", "Enero", "Febrero", "Abril",
            "Marzo","Mayo","Junio", "Julio","Agosto",
            "Septiembre","Octubre","Noviembre","Diciembre" }));
con2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        con2ActionPerformed(evt);
    }
    });

    con1.setText("Mes");

    con3.setText("Mes");

    con4.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
        "None", "Enero", "Febrero", "Abril",
        "Marzo","Mayo","Junio", "Julio","Agosto",
        "Septiembre","Octubre","Noviembre","Diciembre" }));
con4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
    con4ActionPerformed(evt);
    }
    });

    jPanel1.setBackground(new java.awt.Color(143, 0, 0));

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18));
    jLabel3.setForeground(new java.awt.Color(255, 255, 255));
    jLabel3.setText("Gasto");

    lnum.setFont(new java.awt.Font("Tahoma", 1, 18));
    lnum.setForeground(new java.awt.Color(255, 255, 255));
    lnum.setText(" ");

    lnum1.setFont(new java.awt.Font("Tahoma", 1, 18));
    lnum1.setForeground(new java.awt.Color(255, 255, 255));
    lnum1.setText(" ");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel3)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 417, Short.MAX_VALUE)
            .addComponent(lnum1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(lnum)
            .addGap(87, 87, 87))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(lnum)
                .addComponent(lnum1))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/regresar.png"))); // NOI18N
    jButton3.setText("Regresar");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });

    jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/furgoni.png"))); // NOI18N
    jButton7.setText("Furgones");
    jButton7.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton7ActionPerformed(evt);
        }
    });

    jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/caberzal.png"))); // NOI18N
    jButton6.setText("Cabezales");
    jButton6.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton6ActionPerformed(evt);
        }
    });

    jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/camion.png"))); // NOI18N
    jButton5.setText("Camiones");
    jButton5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton5ActionPerformed(evt);
        }
    });

    jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/1316298596_print_16x16.gif"))); // NOI18N
    jButton8.setText("Imprimir");
    jButton8.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton8ActionPerformed(evt);
        }
    });

    jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/1316393209_mail_new.png"))); // NOI18N
    jButton9.setText("Correo");
    jButton9.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton9ActionPerformed(evt);
        }
    });

    cgra.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/1316513843_column_chart.png"))); // NOI18N
    cgra.setText("Grafica");
    cgra.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cgraActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(layout.createSequentialGroup()
            .addGap(10, 10, 10)
            .addComponent(bcon, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(113, 113, 113)
            .addComponent(con1)
            .addGap(18, 18, 18)
            .addComponent(con2, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(46, 46, 46)
            .addComponent(con3)
            .addGap(37, 37, 37)
            .addComponent(con4, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(10, 10, 10)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(bnew, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                        .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                        .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                        .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                        .addComponent(jButton9, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                        .addComponent(cgra, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(bcon)
                .addGroup(layout.createSequentialGroup()
                    .addGap(5, 5, 5)
                    .addComponent(con1))
                .addGroup(layout.createSequentialGroup()
                    .addGap(2, 2, 2)
                    .addComponent(con2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(5, 5, 5)
                    .addComponent(con3))
                .addGroup(layout.createSequentialGroup()
                    .addGap(2, 2, 2)
                    .addComponent(con4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(11, 11, 11)
                    .addComponent(bnew, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(6, 6, 6)
                    .addComponent(jButton5)
                    .addGap(11, 11, 11)
                    .addComponent(jButton6)
                    .addGap(11, 11, 11)
                    .addComponent(jButton7)
                    .addGap(6, 6, 6)
                    .addComponent(jButton8)
                    .addGap(11, 11, 11)
                    .addComponent(jButton9)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(cgra)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 75, Short.MAX_VALUE)
                    .addComponent(jButton3))
                .addGroup(layout.createSequentialGroup()
                    .addGap(6, 6, 6)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)))
            .addContainerGap())
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bconActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bconActionPerformed
if (mesinicial != 0 && mesfinal != 0){
    
        consulta = "Select * from Gasto where fecha >= 2011/"+mesinicial+"/01 AND fecha <=2011/"+mesfinal+"/01";
        try {
           
            tableModel = new ResultSetTableModel( consulta );
             resultTable.setModel(tableModel);
        } catch (SQLException ex) {
            Logger.getLogger(Gasto.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Gasto.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Gasto.class.getName()).log(Level.SEVERE, null, ex);
        }
}
       
    }//GEN-LAST:event_bconActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void bnewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnewActionPerformed
       dispose();
        NuevoGasto va = new NuevoGasto(num,tipo,kilometraje);
        va.setVisible(true);
    }//GEN-LAST:event_bnewActionPerformed

    private void resultTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultTableMouseClicked
       Mostrar(evt); // metodo al dar click
    }//GEN-LAST:event_resultTableMouseClicked

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
tipo="Furgon";
        consulta = "Select * from gasto where tipovehiculo like 'Furgon'";
        try {
            
            tableModel = new ResultSetTableModel( consulta );
            resultTable.setModel(tableModel);
        } catch (SQLException ex) {
            Logger.getLogger(Gasto.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Gasto.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Gasto.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_jButton7ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
tipo="Cabezal";
        consulta = "Select * from gasto where tipovehiculo like 'Cabezal'";
        try {
            
            tableModel = new ResultSetTableModel( consulta );
            resultTable.setModel(tableModel);
        } catch (SQLException ex) {
            Logger.getLogger(Gasto.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Gasto.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Gasto.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_jButton6ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
tipo="Camion";
        consulta = "Select * from gasto where tipovehiculo like 'Camion'";
        try {
            
            tableModel = new ResultSetTableModel( consulta );
            resultTable.setModel(tableModel);
        } catch (SQLException ex) {
            Logger.getLogger(Gasto.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Gasto.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Gasto.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_jButton5ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        file=Archivo.TipoC("ListadoGasto",tipo,true);
        Archivo.Abrir(file);
}//GEN-LAST:event_jButton8ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        file=Archivo.TipoC("ListadoGasto",tipo,true);
        Correo.EnviarArchivo(Inicio.usuario,file);
}//GEN-LAST:event_jButton9ActionPerformed

    private void cgraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cgraActionPerformed
       file=Archivo.TipoD("EstadisticaGasto",tipo,num);
        Archivo.Abrir(file);
    }//GEN-LAST:event_cgraActionPerformed

    private void con2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_con2ActionPerformed
             
        mesinicial = con2.getSelectedIndex();
    }//GEN-LAST:event_con2ActionPerformed

    private void con4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_con4ActionPerformed
      mesfinal = con4.getSelectedIndex();
    }//GEN-LAST:event_con4ActionPerformed
 public void Mostrar(MouseEvent evt)
            {
                    int fila = resultTable.rowAtPoint(evt.getPoint());
                if (fila > -1)
                {
                      dispose();
                      Object objeto = tableModel.getValueAt(fila, 0);

                     String ja = objeto.toString();
                     int n=Integer.parseInt(ja);


                      GastoSelect va = new GastoSelect (n);
                      va.setVisible(true);
                      va.setResizable(false);

                     
                }
            }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               
                    new Gasto().setVisible(true);
            
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bcon;
    private javax.swing.JButton bnew;
    private javax.swing.JButton cgra;
    private javax.swing.JLabel con1;
    private javax.swing.JComboBox con2;
    private javax.swing.JLabel con3;
    private javax.swing.JComboBox con4;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lnum;
    private javax.swing.JLabel lnum1;
    private final javax.swing.JTable resultTable = new javax.swing.JTable(tableModel);
    // End of variables declaration//GEN-END:variables

}
